package com.fastadmin.wechat;

import com.fastadmin.common.mapper.JsonMapper;
import com.fastadmin.pojo.FUser;
import com.fastadmin.thread.FUserThreadLocal;
import org.springframework.http.HttpStatus;
import sun.net.ftp.FtpClient;

import java.util.Objects;

import static com.google.common.collect.ImmutableMap.of;

public class FBaseController {
    protected static JsonMapper binder = JsonMapper.nonDefaultMapper();
    public static final JsonMapper binder2 = JsonMapper.nonDefaultMapper();
    protected static HttpStatus HTTP_STATUS;

    public FUser getUer() {
        FUser fUser = FUserThreadLocal.get();
        return fUser;
    }
    protected String ok() {
        return binder2.toJson(of("code", 1, "msg", HTTP_STATUS.OK.getReasonPhrase()));
    }

    protected String ok(Object data) {
        if (Objects.isNull(data)) {
            return ok();
        }
        return binder2.toJson(of("code", HTTP_STATUS.OK.value(), "msg", HTTP_STATUS.OK.getReasonPhrase(), "data", data));
    }

    protected String error() {
        return binder.toJson(of("code", HTTP_STATUS.INTERNAL_SERVER_ERROR.value(), "msg", HTTP_STATUS.INTERNAL_SERVER_ERROR.getReasonPhrase()));
    }

    protected String error(Object data) {
        if (Objects.isNull(data)) {
            return error();
        }
        return binder.toJson(of("code", HTTP_STATUS.INTERNAL_SERVER_ERROR.value(), "msg", HTTP_STATUS.INTERNAL_SERVER_ERROR.getReasonPhrase(), "data", data));
    }

}
